package data_structure;

public class leetcode_0236 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode[] result=new TreeNode[1];
        result[0]=root;
        low(root,p,q,result);
        return result[0];
    }
    public boolean low(TreeNode node, TreeNode p, TreeNode q,TreeNode[] result){

        if(node==null) return false;
        boolean flag1=low(node.left,p,q,result);
        boolean flag2=low(node.right,p,q,result);
        if(flag1&&flag2){
            result[0]=node;
            return false;
        }
        if(flag1||flag2){
            if(node==p||node==q){
                result[0]=node;
                return false;
            }
            return true;
        }
        if(node==p||node==q) return true;
        return false;
    }
}
